package stockPrice;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import mainBoerse.Share;
import exceptions.WrongShareName;

public class HistoricalStockPriceProvider extends StockPriceProvider {

	String nextLine;
	URL url = null;
	URLConnection urlConn = null;
	InputStreamReader inStream = null;
	BufferedReader buff = null;
	HashMap<String, ArrayList<Float>> prices;
	int counter = 1;
	/**
	 * names and links for real courses
	 */
	public HistoricalStockPriceProvider() {
		SetUp();
		readCourses(
				"BMW",
				"http://www.google.com/finance/historical?q=ETR%3ABMW&ei=uj6TUfinOYerwAPnaA&output=csv");
		readCourses(
				"AUDI",
				"http://www.google.com/finance/historical?q=ETR%3AAUD&ei=uj6TUfinOYerwAPnaA&output=csv");
		readCourses(
				"VW",
				"http://www.google.com/finance/historical?q=OTCMKTS%3AVLKAY&ei=iAx_U7DaMIzCwAOx4YG4Cw&output=csv");
		// readCourses("PORSCHE",
		// "http://www.google.com/finance/historical?q=OTCMKTS%3APOAHY&ei=iw1_U5jaCcqJwAOF3oGoDA&output=csv");
	}
	
	/**
	 * Setup for HistocricalStockPriceProvider
	 * fills prices HashMap with 
	 * available Shares
	 * 
	 */
	public void SetUp() {
		this.prices = new HashMap<String, ArrayList<Float>>();
		for (Share share : StockPriceProvider.getSHARES()) {
			this.prices.put(share.getAktienName(), new ArrayList<>());
		}

	}
	
	/**
	 * Fills prices HashMa with data
	 * from CSV file 
	 * @param shareName - share for prices
	 * @param urlToCSV - URL to CSV file
	 */
	public void readCourses(String shareName, String urlToCSV) {
		try {
			// Create the URL obect that points
			// at the default file index.html
			url = new URL(urlToCSV);
			urlConn = url.openConnection();
			inStream = new InputStreamReader(urlConn.getInputStream());
			buff = new BufferedReader(inStream);
			boolean firstline = true;
			// Read and print the lines from index.html
			while (true) {

				nextLine = buff.readLine();
				if (firstline) {
					firstline = false;
					continue;
				}
				if (nextLine != null) {
					String[] splitted = nextLine.split(",");
					// System.out.println(nextLine);
					this.prices.get(shareName).add(
							Float.parseFloat(splitted[4]));

				} else {
					break;
				}

			}
		} catch (MalformedURLException e) {
			System.out.println("Please check the URL:" + e.toString());
		} catch (IOException e1) {
			System.out.println("Can't read  from the Internet: "
					+ e1.toString());
		}
		// System.out.print(prices.get("BMW").toString());
	}
	/**
	 * updates for sharerates with "real" courses
	 */
	@Override
	public void updateShareRates() {
		for (int i = 0; i < StockPriceProvider.getSHARES().size(); i++) {
			if (prices.containsKey(StockPriceProvider.getSHARES().get(i)
					.getAktienName())) {
				if (counter == prices.get("BMW").size() - 1)
					counter = 0;
				else
					counter++;
				StockPriceProvider
						.getSHARES()
						.get(i)
						.setCourse(
								prices.get(
										StockPriceProvider.getSHARES().get(i)
												.getAktienName()).get(counter));
			}
		}

	}

	@Override
	protected void updateShareRate(Share share) throws WrongShareName {
		// TODO Auto-generated method stub

	}

}
